<template>
  <el-dialog v-model="dialogVisible" title="发票审核">
    <el-form ref="ruleFormRef" :model="ruleForm" label-width="120px">
      <el-form-item label="抬头类型:">
        <span>{{ invoice.invoiceTitleType === "person" ? "个人" : "单位" }}</span>
      </el-form-item>
      <el-form-item label="发票抬头:">
        <span>{{ invoice.invoiceTitle }}</span>
      </el-form-item>
      <el-form-item label="发票税号:">
        <span>{{ invoice.invoiceTaxNumber }}</span>
      </el-form-item>
      <el-form-item label="邮箱:">
        <span>{{ invoice.mailbox }}</span>
      </el-form-item>
      <el-form-item label="注册地址:" v-if="invoice.invoiceAddress">
        <span>{{ invoice.invoiceAddress }}</span>
      </el-form-item>
      <el-form-item label="注册电话:" v-if="invoice.invoicePhoneNumber">
        <span>{{ invoice.invoicePhoneNumber }}</span>
      </el-form-item>
      <el-form-item label="开户银行:" v-if="invoice.invoiceBank">
        <span>{{ invoice.invoiceBank }}</span>
      </el-form-item>
      <el-form-item label="银行账户:" v-if="invoice.invoiceBankNumber">
        <span>{{ invoice.invoiceBankNumber }}</span>
      </el-form-item>
      <el-form-item label="开票状态:" prop="invoiceStatus">
        <el-select v-model="ruleForm.invoiceStatus" placeholder="请选择开票状态">
          <el-option v-for="item in state.optionStatus" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="resetForm(ruleFormRef)">取消</el-button>
        <el-button type="primary" @click="submitRules(ruleFormRef)">
          确定
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref, watch } from "vue";
import { ElMessage, FormInstance } from "element-plus";
import { InvoiceStatusEnum } from "@/views/area/config/enum";
import { postOrderInvoice } from "@/api/area/index";

const props = defineProps(["content", "invoice"]);
const dialogVisible = ref(false);
const ruleFormRef = ref<FormInstance>();
const ruleForm = reactive({
  invoiceStatus: "",
  orderVenueId: "",
  orderInvoiceId: "",
});

watch(() => props.invoice.invoiceStatus, (newVal, oldVal) => {
  ruleForm.invoiceStatus = newVal;
})
const state = reactive({
  optionStatus: [
    {
      label: "待处理",
      value: InvoiceStatusEnum.PENDING,
    },
    {
      label: "审核中",
      value: InvoiceStatusEnum.AUDITING,
    },
    {
      label: "已完成",
      value: InvoiceStatusEnum.COMPLETED,
    },
  ],
});

// 提交发票审核按钮
const submitRules = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      ruleForm.orderVenueId = props.content.orderVenueId;
      ruleForm.orderInvoiceId = props.invoice.orderInvoiceId;
      postOrderInvoice(ruleForm).then(res => {
        if (res.code == 200) {
          ElMessage({
            type: 'success',
            message: res.msg,
          })
        }
        formEl.resetFields();
        dialogVisible.value = false;
      })
    }
  })
};

// 取消按钮
const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
  dialogVisible.value = false;
};

defineExpose({
  dialogVisible,
});
</script>
<style lang="scss" scoped>
.dialog-footer button:first-child {
  margin-right: 10px;
}
</style>
